package edu.pitt.dbmi.monarch.rest;

import org.apache.log4j.Logger;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Author : Jeremy Espino MD
 * Created  8/26/13 11:26 AM
 *
 * Program environment parameters are stored as a properties file "monarch-ui.properties" in the root of the classpath.
 *
 */
public class Env {

    public static final String ENV_PROPERTIES_FILENAME = "monarch-ui.properties";

    public static final String WORK_DIRECTORY_PROPERTY = "workDirectory";
    public static final String ONTOLOGY = "ontology";
    public static final String MOUSE_MAP_FILE = "mouseMap";
    public static final String HP_MAP_FILE = "hpMap";
    public static final String DX_MOUSE_CACHE = "dxMouseCache";
    public static final String DX_DX_HPO_CACHE = "dxMouseCache";

    private Thread runningThread;
    private SimSearch simSearcher;

    private Properties properties = new Properties();
    private static Logger logger = Logger.getLogger(Env.class.getCanonicalName());

    private static class EnvHolder {
        public static final Env INSTANCE = new Env();
    }

    private Env() {
        try {
            properties = getPropertiesFromClasspath(ENV_PROPERTIES_FILENAME);
        } catch (IOException e) {
            logger.error(e);
        }
    }

    public Thread getRunningThread() {
        return runningThread;
    }

    public void setRunningThread(Thread runningThread) {
        this.runningThread = runningThread;
    }

    public SimSearch getSimSearcher() {
        return simSearcher;
    }

    public void setSimSearcher(SimSearch simSearcher) {
        this.simSearcher = simSearcher;
    }

    public static Env getInstance() {
        return EnvHolder.INSTANCE;
    }

    public String getProperty(String parameter) {
        return properties.getProperty(parameter);
    }

    private Properties getPropertiesFromClasspath(String propFileName) throws IOException {
        Properties props = new Properties();
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(propFileName);

        if (inputStream == null) {
            throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
        }

        props.load(inputStream);

        return props;
    }

}
